//
//  UIView+common.m
//  TojoyCloud
//
//  Created by 李亚川 on 2020/4/29.
//  Copyright © 2020 tianjiu. All rights reserved.
//

#import "UIView+common.h"
#import <objc/runtime.h>
//#import <Macro/CommonGlobalMacro.h>
//#import "UIView+Additions.h"

@implementation UIView (common)

- (void)setVar_typeTag:(NSInteger)typeTag {
    objc_setAssociatedObject(self, @"typeTag", @(typeTag), OBJC_ASSOCIATION_ASSIGN);
}

- (NSInteger)var_typeTag {
    NSNumber * var_aNum = objc_getAssociatedObject(self, @"typeTag");
    return [var_aNum integerValue];
}


//画虚线
- (void)wb_drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *var_shapeLayer = [CAShapeLayer layer];
    [var_shapeLayer setBounds:lineView.bounds];
    [var_shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [var_shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [var_shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [var_shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [var_shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [var_shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef var_path = CGPathCreateMutable();
    CGPathMoveToPoint(var_path, NULL, 0, 0);
    CGPathAddLineToPoint(var_path, NULL,CGRectGetWidth(lineView.frame), 0);
    [var_shapeLayer setPath:var_path];
    CGPathRelease(var_path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:var_shapeLayer];
}



/// 找到自己的vc
- (UIViewController *)wb_viewController{
    for (UIView* next = self; next; next = next.superview) {
        UIResponder* var_nextResponder = [next nextResponder];
        if ([var_nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)var_nextResponder;
        }
    }
    return nil;
}

/// 添加由浅到深的蓝色渐变色，添加之前需要设置self.frame
- (CAGradientLayer *)wb_doAddGrayGradientLayer
{
    [self wb_doRemoveGraditentLayer];
    // 渐变色,由浅到深
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1213;
    var_gradientLayer.frame = self.bounds;
    
    var_gradientLayer.colors = @[(__bridge id)UIColorFromRGBA(0x000000, 0.39).CGColor, (__bridge id)UIColorFromRGBA(0x000000, 0).CGColor];
    var_gradientLayer.startPoint = CGPointMake(0, 0.5);
    var_gradientLayer.endPoint = CGPointMake(1, 0.5);
    
    return var_gradientLayer;
}

/// 添加由浅到深的蓝色渐变色，添加之前需要设置self.frame
- (CAGradientLayer *)wb_doAddPurpleGradientLayer
{
    [self wb_doRemoveGraditentLayer];
    // 渐变色,由浅到深
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1214;
    var_gradientLayer.frame = self.bounds;
    
    var_gradientLayer.colors = @[(__bridge id)UIColorFromRGBA(0xF7F7FF, 1.0).CGColor, (__bridge id)UIColorFromRGBA(0xE6E6FF, 1.0).CGColor];
    var_gradientLayer.startPoint = CGPointMake(0.5, 0);
    var_gradientLayer.endPoint = CGPointMake(0.5, 1);
    
    return var_gradientLayer;
}

/// 移除渐变色layer
- (void)wb_doRemoveGraditentLayer
{
    // 渐变色,由浅到深
    for (CAGradientLayer * var_layer in self.layer.sublayers) {
        if ([var_layer isKindOfClass:CAGradientLayer.class]) {
            if (var_layer.var_Tag == 1212 ||
                var_layer.var_Tag == 1213 ||
                var_layer.var_Tag == 1214 ) {
                [var_layer removeFromSuperlayer];
                return;
            }
        }
    }
   
}


/// 添加垂直渐变色，添加之前需要设置self.frame
/// @param startColor 开始颜色
/// @param endColor 结束颜色
- (CAGradientLayer *)wb_addVerticalGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
    return [self wb_doAddGradientLayerStartColor:startColor endColor:endColor startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1.0)];
}
/// 添加水平渐变色，添加之前需要设置self.frame
/// @param startColor 开始颜色
/// @param endColor 结束颜色
- (CAGradientLayer *)wb_addHorizontalGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor
{
    return [self wb_doAddGradientLayerStartColor:startColor endColor:endColor startPoint:CGPointMake(0, 0.5) endPoint:CGPointMake(1, 0.5)];
}

/// 添加渐变色，添加之前需要设置self.frame
/// @param startColor 开始颜色
/// @param endColor 结束颜色
/// @param startPoint 开始点
/// @param endPoint 结束点
- (CAGradientLayer *)wb_doAddGradientLayerStartColor:(CGColorRef)startColor endColor:(CGColorRef)endColor startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint
{
    [self wb_doRemoveGraditentLayer];
    // 渐变色
    CAGradientLayer *var_gradientLayer = [CAGradientLayer layer];
    [self.layer insertSublayer:var_gradientLayer atIndex:0];
    var_gradientLayer.var_Tag = 1212;
    var_gradientLayer.frame = self.bounds;
    var_gradientLayer.colors = @[(__bridge id)startColor, (__bridge id)endColor];
    var_gradientLayer.startPoint = startPoint;
    var_gradientLayer.endPoint = endPoint;
    return var_gradientLayer;
}

/// 顶部添加一个透明渐变的图层，直播间的聊天室
/// @param multiple 多样的
- (void)wb_doAddAlphaGradientLayer:(CGFloat)multiple
{
    [self wb_doAddAlphaGradientLayer:multiple direction:0];
}
- (void)wb_doAddAlphaGradientLayer:(CGFloat)multiple direction:(NSInteger)direction
{
    CAGradientLayer *var_layer = [[CAGradientLayer alloc] init];
    var_layer.colors = @[
                     (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                     (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                     (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor
                     ];
    var_layer.locations = @[@0, @(0.1* multiple), @(0.4 * multiple)];
    
    switch (direction) {
        case 0://从上至下
            var_layer.startPoint = CGPointMake(0.5, 0);
            var_layer.endPoint = CGPointMake(0.5, 1);
            break;
        case 1://从左至右
            var_layer.startPoint = CGPointMake(0, 0.5);
            var_layer.endPoint = CGPointMake(1, 0.5);
//            layer.frame = CGRectMake(0, 0, CGRectGetWidth(layer.frame) * multiple, CGRectGetHeight(layer.frame));
            break;
        case 2://从下至上
            var_layer.startPoint = CGPointMake(0.5, 1);
            var_layer.endPoint = CGPointMake(0.5, 0);
            break;
        case 3://从右至左
            var_layer.startPoint = CGPointMake(1, 0.5);
            var_layer.endPoint = CGPointMake(0, 0.5);
//            layer.frame = CGRectMake(CGRectGetWidth(layer.frame) * (1-multiple), 0, CGRectGetWidth(layer.frame) * multiple, CGRectGetHeight(layer.frame));
            break;
        case 4://从右至左
            var_layer.colors = @[
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:1.0f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.2f].CGColor,
                             (__bridge id)[UIColor colorWithWhite:0 alpha:0.0f].CGColor,
                             ];
            var_layer.locations = @[@0, @(0.1* multiple), @(0.4 * multiple),@(1 - 0.4 * multiple),@(1 - 0.1 * multiple),@1];
            var_layer.startPoint = CGPointMake(0, 0.5);
            var_layer.endPoint = CGPointMake(1, 0.5);
//            layer.frame = CGRectMake(CGRectGetWidth(layer.frame) * (1-multiple), 0, CGRectGetWidth(layer.frame) * multiple, CGRectGetHeight(layer.frame));
            break;
            
        default:
            var_layer.startPoint = CGPointMake(0.5, 0);
            var_layer.endPoint = CGPointMake(0.5, 1);
            break;
    }
    var_layer.frame = self.bounds;
    self.layer.mask = var_layer;
}

/// 边框阴影
/// @param aCornerRadius 半径
/// @param color 颜色
- (void)wb_setupShadowWithCornerRadius:(CGFloat)aCornerRadius color:(UIColor *)color
{
    self.layer.cornerRadius = aCornerRadius;
    self.layer.shadowOpacity = 1.0f;
    self.layer.shadowRadius = aCornerRadius;
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = CGSizeMake(0, 1);
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.layer.bounds].CGPath;
}


/// 从UISearchBar里面获取输入框控件
/// @param aSearchBar 搜索框
+ (UITextField *)wb_doGetTextFiledFromSearchBar:(UISearchBar *)aSearchBar
{
    UITextField *var_searchField = nil;
    for (UIView *var_aView in [[aSearchBar subviews].firstObject subviews]) {
       if ([var_aView isKindOfClass:[UITextField class]]) {
           var_searchField = (UITextField *)var_aView;
           break;
       }
       for (UIView *aSubView in var_aView.subviews) {
           if ([aSubView isKindOfClass:[UITextField class]]) {
               var_searchField = (UITextField *)aSubView;
               break;
           }
       }
    }
    return var_searchField;
}

@end


@implementation CALayer (TJTagLayer)

-(NSInteger)var_Tag
{
    return  [objc_getAssociatedObject(self, @"layerTag") integerValue];
}
-(void)setVar_Tag:(NSInteger)mTag
{
    objc_setAssociatedObject(self, @"layerTag", @(mTag), OBJC_ASSOCIATION_RETAIN);
}
@end
